using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.ServiceProcess;

namespace Emlyn.Applications.Servant
{
	/// <summary>
	/// Summary description for frmAllServices.
	/// </summary>
	public class frmAllServices : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Panel panel1;
		private System.Windows.Forms.Button btnOk;
		private System.Windows.Forms.Button btnCancel;
		private System.Windows.Forms.Panel panel2;
		private System.Windows.Forms.ListBox lbAllServices;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public frmAllServices()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.panel1 = new System.Windows.Forms.Panel();
            this.btnOk = new System.Windows.Forms.Button();
            this.btnCancel = new System.Windows.Forms.Button();
            this.panel2 = new System.Windows.Forms.Panel();
            this.lbAllServices = new System.Windows.Forms.ListBox();
            this.panel1.SuspendLayout();
            this.panel2.SuspendLayout();
            this.SuspendLayout();
            // 
            // panel1
            // 
            this.panel1.Controls.Add(this.btnOk);
            this.panel1.Controls.Add(this.btnCancel);
            this.panel1.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.panel1.Location = new System.Drawing.Point(0, 213);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(324, 36);
            this.panel1.TabIndex = 1;
            // 
            // btnOk
            // 
            this.btnOk.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.btnOk.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.btnOk.Location = new System.Drawing.Point(160, 8);
            this.btnOk.Name = "btnOk";
            this.btnOk.Size = new System.Drawing.Size(75, 23);
            this.btnOk.TabIndex = 1;
            this.btnOk.Text = "&Ok";
            // 
            // btnCancel
            // 
            this.btnCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.btnCancel.Location = new System.Drawing.Point(240, 8);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.Size = new System.Drawing.Size(75, 23);
            this.btnCancel.TabIndex = 0;
            this.btnCancel.Text = "&Cancel";
            // 
            // panel2
            // 
            this.panel2.Controls.Add(this.lbAllServices);
            this.panel2.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panel2.Location = new System.Drawing.Point(0, 0);
            this.panel2.Name = "panel2";
            this.panel2.Size = new System.Drawing.Size(324, 213);
            this.panel2.TabIndex = 2;
            // 
            // lbAllServices
            // 
            this.lbAllServices.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lbAllServices.Location = new System.Drawing.Point(0, 0);
            this.lbAllServices.Name = "lbAllServices";
            this.lbAllServices.Size = new System.Drawing.Size(324, 212);
            this.lbAllServices.TabIndex = 0;
            this.lbAllServices.SelectedIndexChanged += new System.EventHandler(this.lbAllServices_SelectedIndexChanged);
            // 
            // frmAllServices
            // 
            this.AcceptButton = this.btnOk;
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.CancelButton = this.btnCancel;
            this.ClientSize = new System.Drawing.Size(324, 249);
            this.Controls.Add(this.panel2);
            this.Controls.Add(this.panel1);
            this.MinimumSize = new System.Drawing.Size(224, 184);
            this.Name = "frmAllServices";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
            this.Text = "Choose a Service...";
            this.Load += new System.EventHandler(this.frmAllServices_Load);
            this.panel1.ResumeLayout(false);
            this.panel2.ResumeLayout(false);
            this.ResumeLayout(false);

		}
		#endregion

		public static ServiceController Execute()
		{
			ServiceController retval = null;

			frmAllServices frm = new frmAllServices();

			frm.UpdateControls();

			if (frm.ShowDialog() == DialogResult.OK)
			{
				ServiceRep lserviceRep = frm.lbAllServices.SelectedItem as ServiceRep;
				retval = lserviceRep.Service;
			}

			return retval;
		}

		void UpdateControls()
		{
			foreach (ServiceController lservice in System.ServiceProcess.ServiceController.GetServices())
			{
				ServiceRep lserviceRep = new ServiceRep();
				lserviceRep.Service = lservice;

				lbAllServices.Items.Add(lserviceRep);
			}
			lbAllServices.Sorted = true;

			UpdateOKCancel();
		}

		void UpdateOKCancel()
		{
			btnOk.Enabled = lbAllServices.SelectedItem != null;
		}

		private void lbAllServices_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			UpdateOKCancel();
		}

		private void frmAllServices_Load(object sender, System.EventArgs e)
		{
		
		}

		private void listView1_SelectedIndexChanged(object sender, System.EventArgs e)
		{
		
		}
	}

	public class ServiceRep
	{
		ServiceController _service = null;

		public ServiceController Service
		{
			get
			{
				return _service;
			}
			set
			{
				_service = value;
			}
		}

		public override string ToString()
		{
			return _service.DisplayName;
		}
	}
}
